package dm.ass2.io;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.Writable;

public class CodebookItemsWritable implements Writable {
	private int itemClusterId;
	private int[] itemIds;
	private float[] userClusterRating;
	
	public CodebookItemsWritable() {
		super();
	}

	public CodebookItemsWritable(int itemClusterId, int[] itemIds, float[] userClusterRating) {
		super();
		this.itemClusterId = itemClusterId;
		this.itemIds = itemIds;
		this.userClusterRating = userClusterRating;
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		this.itemClusterId = in.readInt();
		
		// Reading the user ids in this user cluster.
		int numOfUsersInCluster = in.readInt();
		this.itemIds = new int[numOfUsersInCluster];
		for (int i = 0; i < this.itemIds.length; i++) {
			this.itemIds[i] = in.readInt();
		}

		// Reading the rating of item clusters.
		int numberOfItemsCluster = in.readInt();
		this.userClusterRating = new float[numberOfItemsCluster];
		for (int i = 0; i < this.userClusterRating.length; i++) {
			this.userClusterRating[i] = in.readFloat();
		}

	}

	@Override
	public void write(DataOutput out) throws IOException {
		out.writeInt(this.itemClusterId);
		out.writeInt(this.itemIds.length);
		for (int i = 0; i < this.itemIds.length; i++) {
			out.writeInt(this.itemIds[i]);
		}
		
		out.writeInt(this.userClusterRating.length);
		for (int i = 0; i < this.userClusterRating.length; i++) {
			out.writeFloat(this.userClusterRating[i]);
		}

	}

	public int getItemClusterId() {
		return itemClusterId;
	}

	public void setItemIds(int[] itemIds) {
		this.itemIds = itemIds;
	}

	public void setUserClusterRating(float[] userClusterRating) {
		this.userClusterRating = userClusterRating;
	}

	public int[] getItemIds() {
		return itemIds;
	}

	public float[] getUserClusterRating() {
		return userClusterRating;
	}

}
